// ignore_for_file: unrelated_type_equality_checks

/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-09-01 15:35:50
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-09 13:41:30
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/battery_pack_detail/components/battery_pack_widget.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yt_devops/pages/devices/device_tab/components/detail_grid_widget.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_util.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/socket/use_subscribe.dart';
import '../../../model/device_items_group_model.dart';
import '../battery_pack_detail_model.dart';

class BatteryPackWidget extends StatefulWidget {
  final int deviceId;
  final int productId;
  final ConnectStatusType? connectStatus;
  const BatteryPackWidget(
    this.deviceId,
    this.productId,
    this.connectStatus, {
    super.key,
  });

  @override
  State<BatteryPackWidget> createState() => _BatteryPackWidgetState();
}

class _BatteryPackWidgetState extends State<BatteryPackWidget> {
  List<DeviceItemsGroupModel> group = [];

  UseSubsribe subsribe = UseSubsribe();

  late StreamController<Map<String, dynamic>> streamController;

  @override
  void initState() {
    super.initState();

    streamController = subsribe.start([widget.deviceId], true);

    group = getGoupList();
  }

  @override
  void dispose() async {
    // subsribe.start([widget.deviceId], false);
    super.dispose();
    await streamController.close();
    await subsribe.closeListen();
  }

  getGoupList() {
    BatteryPackDetailModel model = BatteryPackDetailModel();

    return [
      DeviceItemsGroupModel(
          items: model.getControllItems(widget.productId),
          title: 'siteMonitor.controlInformation'.tr), //控制信息
      DeviceItemsGroupModel(
          items: model.runItems,
          title: 'siteMonitor.statusInformation'.tr), //状态信息
      DeviceItemsGroupModel(
          items: model.maxUnitData, title: 'siteMonitor.monomerInfo'.tr), //单体信息
      DeviceItemsGroupModel(
          items: model.historyItems,
          title: 'siteMonitor.historicalInformation'.tr), //'历史信息'
      DeviceItemsGroupModel(
          items: model.tempItems,
          title: 'siteMonitor.temperatureInformation'.tr), // 温度信息
      DeviceItemsGroupModel(
          items: model.abilityItems,
          title: 'siteMonitor.capabilityInformation'.tr), //能力信息
    ];
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: streamController.stream,
      builder: (context, snapshot) => ListView.builder(
        shrinkWrap: false,
        itemBuilder: (context, index) => DeviceUtil.blockContainer(
          DetailGridWidget(
              isLightTtext: widget.connectStatus != ConnectStatusType.on,
              title: group[index].title,
              items: group[index].items,
              dataSource: snapshot.data),
        ),
        itemCount: group.length,
      ),
    );
  }

  getBmus() {}
}
